var express = require('express')
    , router = express.Router()
    , settings = require('../lib/settings')
    , locale = require('../lib/locale')
    , db = require('../lib/database')
    , lib = require('../lib/explorer')
    , qr = require('qr-image');

function route_get_block(res, blockhash) {
    lib.get_block(blockhash, function (block) {
        if (block != 'There was an error. Check your console.') {
            if (blockhash == settings.genesis_block) {
                res.render('block', {
                    active: 'block',
                    block: block,
                    confirmations: settings.confirmations,
                    txs: 'GENESIS'
                });
            } else {
                db.get_txs(block, function (txs) {
                    if (txs.length > 0) {
                        res.render('block', {
                            active: 'block',
                            block: block,
                            confirmations: settings.confirmations,
                            txs: txs
                        });
                    } else {
                        db.create_txs(block, function () {
                            db.get_txs(block, function (ntxs) {
                                if (ntxs.length > 0) {
                                    res.render('block', {
                                        active: 'block',
                                        block: block,
                                        confirmations: settings.confirmations,
                                        txs: ntxs
                                    });
                                } else {
                                    route_get_index(res, 'Block not found: ' + blockhash);
                                }
                            });
                        });
                    }
                });
            }
        } else {
            route_get_index(res, 'Block not found: ' + blockhash);
        }
    });
}

/* GET functions */

function route_get_tx(res, txid) {
    if (txid == settings.genesis_tx) {
        route_get_block(res, settings.genesis_block);
    } else {
        db.get_tx(txid, function (tx) {
            if (tx) {
                lib.get_blockcount(function (blockcount) {
                    res.render('tx', {
                        active: 'tx',
                        tx: tx,
                        confirmations: settings.confirmations,
                        blockcount: blockcount
                    });
                });
            }
            else {
                lib.get_rawtransaction(txid, function (rtx) {
                    if (rtx.txid) {
                        lib.prepare_vin(rtx, function (vin) {
                            lib.prepare_vout(rtx.vout, rtx.txid, vin, function (rvout, rvin) {
                                lib.calculate_total(rvout, function (total) {
                                    if (!rtx.confirmations > 0) {
                                        var utx = {
                                            txid: rtx.txid,
                                            vin: rvin,
                                            vout: rvout,
                                            total: total.toFixed(8),
                                            timestamp: rtx.time,
                                            blockhash: '-',
                                            blockindex: -1,
                                        };
                                        res.render('tx', {
                                            active: 'tx',
                                            tx: utx,
                                            confirmations: settings.confirmations,
                                            blockcount: -1
                                        });
                                    } else {
                                        var utx = {
                                            txid: rtx.txid,
                                            vin: rvin,
                                            vout: rvout,
                                            total: total.toFixed(8),
                                            timestamp: rtx.time,
                                            blockhash: rtx.blockhash,
                                            blockindex: rtx.blockheight,
                                        };
                                        lib.get_blockcount(function (blockcount) {
                                            res.render('tx', {
                                                active: 'tx',
                                                tx: utx,
                                                confirmations: settings.confirmations,
                                                blockcount: blockcount
                                            });
                                        });
                                    }
                                });
                            });
                        });
                    } else {
                        route_get_index(res, null);
                    }
                });
            }
        });
    }
}

function route_get_index(res, error) {
    res.render('index', {active: 'home', error: error, warning: null});
}

function route_get_address(res, hash, count) {
    console.log("address is : " + hash)

    db.get_address(hash, function (address) {
        if (address) {
            var txs = [];
            var txs_block = [];
            var txs_trans = [];
            var hashes = address.txs.reverse();
            if (address.txs.length < count) {
                count = address.txs.length;
            }

            // db.get_mined_txs_by_address(hash, function (txs) {
            //     txs_block.push(txs);
            // });
            // db.get_incoming_txs_by_address(hash, function (txs) {
            //     txs_trans.push(txs)
            // });
            // db.get_outgoing_txs_by_address(hash,function (txs) {
            //     txs_trans.push(txs)
            // });
            //
            // console.log("txs_block :" + txs_block);
            // console.log("txs_trans :" + txs_trans);


            db.get_mined_txs_by_address(hash, function (txs_mined) {
                for (var i = 0; i < txs_mined.length; i++) {
                    txs_block.push(txs_mined[i]);
                }
                db.get_incoming_txs_by_address(hash, function (txs_in) {
                    for (var i = 0; i < txs_in.length; i++) {
                        txs_trans.push(txs_in[i]);
                    }
                    db.get_outgoing_txs_by_address(hash, function (txs_out) {
                        for (var i = 0; i < txs_out.length; i++) {
                            txs_trans.push(txs_out[i]);
                        }
                        console.log("txs_block lenght :" + txs_block.length);
                        console.log("txs_trans lenght :" + txs_trans.length);
                        // console.log("txs_block :" + txs_block);
                        // console.log("txs_trans :" + txs_trans);

                        txs_block.sort(function (a, b) {
                            return (b.timestamp - a.timestamp);
                        });
                        txs_trans.sort(function (a, b) {
                            return (b.timestamp - a.timestamp);
                        });

                        res.render('address', {
                            active: 'address',
                            address: address,
                            txs_trans: (txs_trans),
                            txs_blocks: (txs_block)
                        });
                    });
                });
            }, function () {

            });

            // lib.syncLoop(count, function (loop) {
            //     var i = loop.iteration();
            //     console.log(i);
            //
            //     db.get_tx(hashes[i].addresses, function (tx) {
            //
            //         console.log(hashes[i].addresses);
            //
            //         if (tx) {
            //             txs.push(tx);
            //
            //             if (tx.vin.length > 0 ) {
            //                 txs_trans.push(tx)
            //                 // console.log( "transactions :" + txs_trans)
            //             } else {
            //                 txs_block.push(tx)
            //                 // console.log( "blocks       :" + txs_block)
            //             }
            //
            //             loop.next();
            //         } else {
            //             loop.next();
            //         }
            //     });
            // }, function () {
            //     console.log("txs :" + txs);
            //     res.render('address', {active: 'address', address: address, txs: txs, txs_blocks: txs_block, txs_trans: txs_trans});
            // });

        } else {
            route_get_index(res, hash + ' not found');
        }
    });
}

/* GET home page. */
router.get('/', function (req, res) {
    route_get_index(res, null);
});

router.get("/blocks", function (req, res) {
    res.render('blocks', {active: 'blocks'});
});

router.get('/info', function (req, res) {
    res.render('info', {active: 'info', address: settings.address, hashes: settings.api});
});

router.get('/markets/:market', function (req, res) {
    var market = req.params['market'];
    if (settings.markets.enabled.indexOf(market) != -1) {
        db.get_market(market, function (data) {
            /*if (market === 'bittrex') {
              data = JSON.parse(data);
            }*/
            console.log(data);
            res.render('./markets/' + market, {
                active: 'markets',
                marketdata: {
                    coin: settings.markets.coin,
                    exchange: settings.markets.exchange,
                    data: data,
                },
                market: market
            });
        });
    } else {
        route_get_index(res, null);
    }
});

router.get('/richlist', function (req, res) {
    if (settings.display.richlist == true) {
        db.get_stats(settings.coin, function (stats) {
            // console.log("stat : " + stats)
            db.get_richlist(settings.coin, function (richlist) {
                // console.log("rishlist" + richlist);
                if (richlist) {
                    db.get_distribution(richlist, stats, function (distribution) {
                        // console.log("distribution" + distribution);
                        res.render('richlist', {
                            active: 'richlist',
                            balance: richlist.balance,
                            received: richlist.received,
                            stats: stats,
                            dista: distribution.t_1_25,
                            distb: distribution.t_26_50,
                            distc: distribution.t_51_75,
                            distd: distribution.t_76_100,
                            diste: distribution.t_101plus,
                            show_dist: settings.richlist.distribution,
                            show_received: settings.richlist.received,
                            show_balance: settings.richlist.balance,
                        });
                    });
                } else {
                    route_get_index(res, null);
                }
            });
        });
    } else {
        route_get_index(res, null);
    }
});

router.get('/movement', function (req, res) {
    res.render('movement', {
        active: 'movement',
        flaga: settings.movement.low_flag,
        flagb: settings.movement.high_flag,
        min_amount: settings.movement.min_amount
    });
});

router.get('/network', function (req, res) {
    res.render('network', {active: 'network'});
});

router.get('/reward', function (req, res) {
    //db.get_stats(settings.coin, function (stats) {
    console.log(stats);
    db.get_heavy(settings.coin, function (heavy) {
        //heavy = heavy;
        var votes = heavy.votes;
        votes.sort(function (a, b) {
            if (a.count < b.count) {
                return -1;
            } else if (a.count > b.count) {
                return 1;
            } else {
                return 0;
            }
        });

        res.render('reward', {active: 'reward', stats: stats, heavy: heavy, votes: heavy.votes});
    });
    //});
});

router.get('/tx/:txid', function (req, res) {
    route_get_tx(res, req.param('txid'));
});

router.get('/block/:hash', function (req, res) {
    route_get_block(res, req.param('hash'));
});

router.get('/address/:hash', function (req, res) {
    route_get_address(res, req.param('hash'), settings.txcount);
});

router.get('/address/:hash/:count', function (req, res) {
    route_get_address(res, req.param('hash'), req.param('count'));
});

router.post('/search', function (req, res) {
    var query = req.body.search;
    if (query.length == 64) {
        if (query == settings.genesis_tx) {
            res.redirect('/block/' + settings.genesis_block);
        } else {
            db.get_tx(query, function (tx) {
                if (tx) {
                    res.redirect('/tx/' + tx.txid);
                } else {
                    lib.get_block(query, function (block) {
                        if (block != 'There was an error. Check your console.') {
                            res.redirect('/block/' + query);
                        } else {
                            route_get_index(res, locale.ex_search_error + query);
                        }
                    });
                }
            });
        }
    } else {
        db.get_address(query, function (address) {
            if (address) {
                res.redirect('/address/' + address.a_id);
            } else {
                lib.get_blockhash(query, function (hash) {
                    if (hash != 'There was an error. Check your console.') {
                        res.redirect('/block/' + hash);
                    } else {
                        route_get_index(res, locale.ex_search_error + query);
                    }
                });
            }
        });
    }
});

router.get('/qr/:string', function (req, res) {
    if (req.param('string')) {
        var address = qr.image(req.param('string'), {
            type: 'png',
            size: 4,
            margin: 1,
            ec_level: 'M'
        });
        res.type('png');
        address.pipe(res);
    }
});

router.get('/ext/summary', function (req, res) {

    lib.get_difficulty(function (difficulty) {
        difficultyHybrid = ''
        if (difficulty['proof-of-work']) {
            console.log(settings.index.difficulty)
            if (settings.index.difficulty == 'Hybrid') {
                difficultyHybrid = 'POS: ' + difficulty['proof-of-stake'];
                difficulty = 'POW: ' + difficulty['proof-of-work'];
            } else if (settings.index.difficulty == 'POW') {
                difficulty = difficulty['proof-of-work'];
            } else {
                difficulty = difficulty['proof-of-stake'];
            }
        }
        lib.get_hashrate(function (hashrate) {
            lib.get_connectioncount(function (connections) {
                lib.get_blockcount(function (blockcount) {
                    lib.get_stakinginfo(function (stakinginfo) {
                        db.get_stats(settings.coin, function (stats) {
                            if (hashrate == 'There was an error. Check your console.') {
                                hashrate = 0;
                            }
                            res.send({
                                data: [{
                                    difficulty: difficulty,
                                    difficultyHybrid: difficultyHybrid,
                                    supply: stats.supply - settings.destroyed.balance + settings.destroyed.diff,
                                    hashrate: hashrate,
                                    lastPrice: stats.last_price,
                                    connections: connections,
                                    blockcount: blockcount,
                                    stakingWeight: stakinginfo.netstakeweight / 100000000
                                }]
                            });
                        });
                    });
                });
            });
        });
    });
});
module.exports = router;
